﻿using System;
using System.Windows;
using MyFloridaHouse.ServiceConn.DistrBldrSvcRef;


namespace MyFloridaHouse.libSvOpenDistricts.DataService
{
    public delegate void ServiceDALGetRegionDataCompletedEventHandler(RegionDataListSTO sto);

    public class PendingGetRegionsDataRequest
    {
        public int layerID;
        public int regionID;

        public PendingGetRegionsDataRequest(int layerID, int regionID)
        {
            this.layerID = layerID;
            this.regionID = regionID;
        }
    }

    public partial class ServiceDAL
    {
        #region events
        public static event ServiceDALGetRegionDataCompletedEventHandler GetRegionDataCompleted;


        private static void RaiseGetRegionDataCompleted(object sender, RegionDataListSTO sto)
        {
            if (GetRegionDataCompleted != null)
            {
                GetRegionDataCompleted(sto);
            }
            else
            {
                // todo: Log this error
                throw (new NotImplementedException("No Subscribers to ServiceDALGetRegionDataCompleted Event"));
            }
        }
        #endregion

#region properties
        private int _getRegionDataActiveCount = 0;
        public int getRegionDataActiveCount
        {
            get { return _getRegionDataActiveCount; }
            set
            {
                if (_getRegionDataActiveCount != value)
                {
                    _getRegionDataActiveCount = value;
                    NotifyPropertyChanged("getRegionDataActiveCount");
                }
            }
        }
#endregion

        private PendingGetRegionsDataRequest pendingRegionDataRequest = null;
        
        // usage: 
        // GetRegionData( iLayer,  iRegionID,new System.EventHandler<GetRegionDataListCompletedEventArgs>(GetRegionDataCompleted) )
        public void GetRegionData(int iLayerID, int iRegionID)
        {
            try
            {
                if (getRegionDataActiveCount>0)
                {
                    // set this incoming request as the next request, forget about a prior request
                    pendingRegionDataRequest = new PendingGetRegionsDataRequest(iLayerID, iRegionID);

                }
                else
                {
                    // execute the request
                    //MyDistrictBuilderServiceClient proxy = new MyDistrictBuilderServiceClient(myRelativeBinding, myRelativeCensusDataServiceAddress);
                    var proxy = new DistrBldrClient(_relativeBindingName);
                    proxy.GetRegionDataListCompleted += ServiceGetRegionDataCompleted;
                    proxy.GetRegionDataListAsync(iLayerID, iRegionID);
                    getRegionDataActiveCount++;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ServiceGetRegionDataCompleted(object sender, GetRegionDataListCompletedEventArgs args)
        {
            getRegionDataActiveCount--;
            try
            {
                if (!IsError("GetRegionDataCompleted", args.Error, args.Result, args.Result.errorMessage))
                {
                    RaiseGetRegionDataCompleted(this, args.Result);
                    if (pendingRegionDataRequest != null)
                    {
                        GetRegionData(pendingRegionDataRequest.layerID, pendingRegionDataRequest.regionID);
                        pendingRegionDataRequest = null;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
   
    }
}
